/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseWorkFlowDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseWorkFlowDao : BaseDao
    {
        public BaseWorkFlowDao()
        {
            base.CurrentTableName = BaseWorkFlowTable.TableName;
        }

        public BaseWorkFlowDao(IDbHelper dbHelper, BaseUserInfo userInfo)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
        }

        public string Add(BaseWorkFlowEntity workFlowEntity)
        {
            string str = string.Empty;
            if (this.Exists(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID, BaseWorkFlowTable.FieldCode, workFlowEntity.Code))
            {
                base.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            if (this.Exists(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID, BaseWorkFlowTable.FieldFullName, workFlowEntity.FullName))
            {
                base.ReturnStatusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            str = this.AddEntity(workFlowEntity);
            base.ReturnStatusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        protected string AddEntity(BaseWorkFlowEntity workFlowEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseWorkFlowTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseWorkFlowTable.TableName);
            builder.SetValue(BaseWorkFlowTable.FieldID, sequence);
            builder.SetValue(BaseWorkFlowTable.FieldCode, workFlowEntity.Code);
            builder.SetValue(BaseWorkFlowTable.FieldFullName, workFlowEntity.FullName);
            builder.SetValue(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID);
            builder.SetValue(BaseWorkFlowTable.FieldSortCode, sequence);
            builder.SetValue(BaseWorkFlowTable.FieldDescription, workFlowEntity.Description);
            builder.SetValue(BaseWorkFlowTable.FieldEnabled, workFlowEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseWorkFlowTable.FieldCreateDate);
            return ((builder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                BaseWorkFlowEntity workFlowEntity = new BaseWorkFlowEntity(row);
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseWorkFlowTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.Delete(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseWorkFlowTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    this.GetFrom(row);
                    num += this.Update(workFlowEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    this.GetFrom(row);
                    num += (this.Add(workFlowEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public override int Delete(string id)
        {
            int num = 0;
            num += DbLogic.Delete(base.DbHelper, BaseUserRoleTable.TableName, BaseUserRoleTable.FieldRoleID, id);
            return (num + DbLogic.Delete(base.DbHelper, BaseWorkFlowTable.TableName, BaseWorkFlowTable.FieldID, id));
        }

        public DataTable GetflowID(string organizeID)
        {
            return DbLogic.GetDT(base.DbHelper, BaseWorkFlowTable.TableName, BaseWorkFlowTable.FieldOrganizeID, organizeID);
        }

        public DataTable GetListByOrganize(string organizeID)
        {
            return DbLogic.GetDT(base.DbHelper, BaseWorkFlowTable.TableName, BaseWorkFlowTable.FieldOrganizeID, organizeID);
        }

        public int Update(BaseWorkFlowEntity workFlowEntity)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseWorkFlowTable.TableName, workFlowEntity.ID, workFlowEntity.ModifyUserID, workFlowEntity.ModifyDate))
            {
                base.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID, BaseWorkFlowTable.FieldFullName, workFlowEntity.FullName, workFlowEntity.ID))
            {
                base.ReturnStatusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            if (this.Exists(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID, BaseWorkFlowTable.FieldCode, workFlowEntity.Code, workFlowEntity.ID))
            {
                base.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(workFlowEntity);
            if (num == 1)
            {
                base.ReturnStatusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            base.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        protected int UpdateEntity(BaseWorkFlowEntity workFlowEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseWorkFlowTable.TableName);
            builder.SetValue(BaseWorkFlowTable.FieldCode, workFlowEntity.Code);
            builder.SetValue(BaseWorkFlowTable.FieldFullName, workFlowEntity.FullName);
            builder.SetValue(BaseWorkFlowTable.FieldOrganizeID, workFlowEntity.OrganizeID);
            builder.SetValue(BaseWorkFlowTable.FieldEnabled, workFlowEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowTable.FieldDescription, workFlowEntity.Description);
            builder.SetValue(BaseWorkFlowTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseWorkFlowTable.FieldModifyDate);
            builder.SetWhere(BaseWorkFlowTable.FieldID, workFlowEntity.ID);
            return builder.EndUpdate();
        }
    }
}